package com.bw.a2208_park_kotlin

import android.Manifest
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Intent
import android.nfc.NfcAdapter
import android.nfc.tech.Ndef
import android.os.Build
import android.os.CountDownTimer
import android.util.Log
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.a2208_park_kotlin.databinding.ActivitySplashBinding
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity

/**
 * 启动页面
 * 业务功能
 * 1.APP介绍（轮播图片，动画，视频）
 * 2.接入广告（开屏广告）
 * 3.第一次安装只会展示App功能，二次打开就有广告
 * 4.选择兴趣标签（为后期内容做推荐）
 *
 */
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {
    @RequiresApi(Build.VERSION_CODES.M)
    override fun initViews() {
        val countDownTimer = object :CountDownTimer(5000,1000){
            override fun onTick(millisUntilFinished: Long) {
                binding.tvTime.text = "${millisUntilFinished/1000}s"
            }

            override fun onFinish() {
                ARouter.getInstance().build(RouterPath.MODULE_MAIN_ACTIVITY).navigation()
                finish()
            }
        }.start()
        binding.tvSkip.setOnClickListener {
            countDownTimer.cancel()
            ARouter.getInstance().build(RouterPath.MODULE_MAIN_ACTIVITY).navigation()
            finish()
        }
        requestPermissions(arrayOf(
            Manifest.permission.ACCESS_COARSE_LOCATION ,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        ),102)

    }

    override fun observeDataChange() {

    }
    override fun getLayoutId(): Int =R.layout.activity_splash

    private fun checkNfcSupport():Boolean{
        val nfcAdapter=NfcAdapter.getDefaultAdapter(this)
        return nfcAdapter?.isEnabled==true
    }

    override fun onResume() {
        super.onResume()
        val nfcAdapter=NfcAdapter.getDefaultAdapter(this)
        val intent=Intent(this,javaClass).apply {
            addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        }
        val pendingIntent =PendingIntent.getActivity(this,0,intent,
            PendingIntent.FLAG_MUTABLE)
        nfcAdapter?.enableForegroundDispatch(this,pendingIntent,null,null)
    }

    override fun onPause() {
        super.onPause()
        NfcAdapter.getDefaultAdapter(this)?.disableForegroundDispatch(this)
    }


}